package wowmobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlayerStats {

    private int id, level, realhp, realmana, realspeed, realattack, realdefence, image;
    private String nick;
    List<HandItem> handitems;
    /*
     * wearitems
     * 0 - broń
     * 1 - zbroja - napierśnik - szata
     * 2 - hełm -czapka
     * 3 - rękawice
     * 4 - buty
     */
    Map<Integer, WearItem> wearitems;
    List<Magic> magiclist;
    PlayerClass pclass;
    private boolean defending;
    private int maxhp, hp, maxmana, mana, defence, attack, speed;

    PlayerStats(int id, String nick, int level, PlayerClass pclass, List<HandItem> handitems, Map<Integer, WearItem> wearitems, List<Magic> magiclist, int image) {
        this.id = id;
        this.nick = nick;
        this.level = level;
        this.pclass = pclass;
        if (handitems != null) {
            this.handitems = new ArrayList<HandItem>(handitems);
        } else {
            this.handitems = new ArrayList<HandItem>();
        }
        if (wearitems != null) {
            if (wearitems.size() == 5) {
                this.wearitems = new HashMap<Integer, WearItem>(wearitems);
            } else {
                if (!wearitems.containsKey(0)) {
                    wearitems.put(0, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 0));
                }
                if (!wearitems.containsKey(1)) {
                    wearitems.put(1, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 1));
                }
                if (!wearitems.containsKey(2)) {
                    wearitems.put(2, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 2));
                }
                if (!wearitems.containsKey(3)) {
                    wearitems.put(3, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 3));
                }
                if (!wearitems.containsKey(4)) {
                    wearitems.put(4, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 4));
                }
            }
        } else {
            this.wearitems = new HashMap<Integer, WearItem>();
            wearitems.put(0, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 0));
            wearitems.put(1, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 1));
            wearitems.put(2, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 2));
            wearitems.put(3, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 3));
            wearitems.put(4, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, 4));
        }
        if (magiclist != null) {
            this.magiclist = new ArrayList<Magic>(magiclist);
        } else {
            this.magiclist = new ArrayList<Magic>();
        }
        setLevel(level);
        this.image = image;
    }

    public void statChange(int hp, int mana, int defence, int attack, int speed, int level) {
        changeHP(hp, level);
        changeMana(mana);
        this.defence += (int) ((double) defence * (double) ((double) level / (double) this.level));
        this.attack += (int) ((double) attack * (double) ((double) level / (double) this.level));
        this.speed += (int) ((double) speed * (double) ((double) level / (double) this.level));
    }

    public void calibrateStats() {
        this.maxhp = getRealHp();
        this.maxmana = getRealMana();
        this.defence = getRealDefence();
        this.attack = getRealAttack();
        this.speed = getRealSpeed();
        for (int i = 0; i <= 4; i++) {
            if (getWearitems().containsKey(i)) {
                this.maxhp += getWearitems().get(i).getHp();
                this.maxmana += getWearitems().get(i).getMana();
                this.defence += getWearitems().get(i).getDefence();
                this.attack += getWearitems().get(i).getAttack();
                this.speed += getWearitems().get(i).getSpeed();
            }
        }
        this.hp = maxhp;
        this.mana = maxmana;
    }

    public void changeHP(int amount, int level) {
        if (amount < 0) {
            if (hp - amount <= maxhp) {
                hp -= amount;
            } else {
                hp = maxhp;
            }
        } else {
            if ((double) hp - (double) amount / (double) defence >= 0) {
                hp -= (int) ((double) amount * (double) ((double) level / (double) this.level) / (double) defence);
            } else {
                hp = 0;
            }
        }
    }

    public boolean isDefending() {
        return defending;
    }

    public void setDefending(boolean defending) {
        this.defending = defending;
    }

    public void changeMana(int amount) {
        if (amount < 0) {
            if (mana - amount <= maxmana) {
                mana -= amount;
            } else {
                mana = getMana();
            }
        } else {
            if (mana - amount >= 0) {
                mana -= amount;
            } else {
                mana = 0;
            }
        }
    }

    public int getDefence() {
        return defence;
    }

    public int getMana() {
        return mana;
    }

    public int getAttack() {
        return attack;
    }

    public int getMaxhp() {
        return maxhp;
    }

    public int getMaxmana() {
        return maxmana;
    }

    public int getHp() {
        return hp;
    }

    public int getSpeed() {
        return speed;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public void setDefence(int defence) {
        this.defence = defence;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setMana(int mana) {
        this.mana = mana;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public void learnMagic(Magic magic) {
        magiclist.add(magic);
    }

    public String getNick() {
        return nick;
    }

    public int getImage() {
        return image;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public List<HandItem> getHanditems() {
        return handitems;
    }

    public List<Magic> getMagiclist() {
        return magiclist;
    }

    public Map<Integer, WearItem> getWearitems() {
        return wearitems;
    }

    public int getId() {
        return id;
    }

    public int getLevel() {
        return level;
    }

    public int getRealHp() {
        return realhp;
    }

    public int getRealMana() {
        return realmana;
    }

    public int getRealSpeed() {
        return realspeed;
    }

    public int getRealAttack() {
        return realattack;
    }

    public int getRealDefence() {
        return realdefence;
    }

    public PlayerClass getPclass() {
        return pclass;
    }

    public void setLevel(int level) {
        setRealHp(pclass.getHp() * level);
        setRealMana(pclass.getMana() * level);
        setRealSpeed(pclass.getSpeed() * level);
        setRealDefence(pclass.getDefence() * level);
        setRealAttack(pclass.getAttack() * level);
        this.level = level;
        calibrateStats();
    }

    public void setRealAttack(int realattack) {
        this.realattack = realattack;
    }

    public void setRealDefence(int realdefence) {
        this.realdefence = realdefence;
    }

    public void setRealHp(int realhp) {
        this.realhp = realhp;
    }

    public void setRealMana(int realmana) {
        this.realmana = realmana;
    }

    public void setRealSpeed(int realspeed) {
        this.realspeed = realspeed;
    }

    public void giveHandItem(HandItem item) {
        handitems.add(item);
    }

    public HandItem checkHandItem(int item) {
        return handitems.get(item);
    }

    public void useHandItem(int item) {
        handitems.remove(item);
    }

    public void giveWearItem(int place, WearItem item) {
        if (wearitems.get(place).equals(new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, place))) {
            wearitems.remove(place);
            wearitems.put(place, item);
        } else {
            discardWearItem(place);
            giveWearItem(place, item);
        }
    }

    public boolean checkWearItem(int place) {
        if (!wearitems.get(place).equals(new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, place))) {
            return true;
        } else {
            return false;
        }
    }

    public void discardWearItem(int place) {
        if (checkWearItem(place)) {
            wearitems.remove(place);
            wearitems.put(place, new WearItem("empty", 0, 0, 0, 0, 0, 0, 0, 0, place));
        }
    }
}
